<?php

namespace App\Models;

use Flower\Core\Repository\Contracts\Transformable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Flower\Core\Repository\Traits\TransformableTrait;

class Order extends Model implements Transformable
{
    protected $table = 'order';
    /**
     * @return array
     */
    protected $guarded = [];

    public function transform()
    {
        return [
            'price' => $this->price,
        ];
    }

    public function setFinishTimeAttribute($value)
    {
        $this->attributes['finish_time'] = is_numeric($value) ? $value : strtotime($value);
    }

    public function setCancelTimeAttribute($value)
    {
        $this->attributes['cancel_time'] = is_numeric($value) ? $value : strtotime($value);
    }

    public function setAcceptTimeAttribute($value)
    {
        $this->attributes['accept_time'] = is_numeric($value) ? $value : strtotime($value);
    }


    public function setAppointmentTimeAttribute($value)
    {
        $this->attributes['accept_time'] = is_numeric($value) ? $value : strtotime($value);
    }


    public function getFinishTimeAttribute()
    {
        return date('Y-m-d H:i:s', $this->attributes['finish_time']);
    }

    public function getCancelTimeAttribute()
    {
        return date('Y-m-d H:i:s', $this->attributes['cancel_time']);
    }

    public function getAcceptTimeAttribute()
    {
        return date('Y-m-d H:i:s', $this->attributes['accept_time']);
    }

    public function getAppointmentTimeAttribute()
    {
        return date('Y-m-d H:i:s', $this->attributes['accept_time']);
    }

    public function store()
    {
        return $this->hasOne(Store::class, 'id', 'store_id');
    }

    public function items()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'id');
    }


    public function itemsOne(){
    	return $this->hasOne(OrderItem::class,'order_id','id');
    }

    public function commentResource()
    {
        return $this->hasManyThrough(CommentResource::class, OrderComment::class);
    }

    public function royalty()
    {
        return $this->hasMany(Royalty::class, 'item_id', 'id');
    }


    //关联理发师
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'barber_id');
    }

    //关联助理
    public function assistant()
    {
        return $this->hasOne(User::class, 'id', 'assistant_id');
    }

    // 关联预约
    public function appointment()
    {
        return $this->hasOne(Appointment::class, 'id', 'appointment_id');
    }

    // 关联用户
    public function member()
    {
        return $this->hasOne(Member::class, 'id', 'member_id');
    }

    /**
     * 订单评论
     */
    public function comment()
    {
        return $this->hasOne(OrderComment::class, 'order_id', 'id');
    }


    // 取消订单
    public function cancel()
    {
        return $this->hasMany(OrderCancel::class, 'order_id', 'id');
    }


    public function memberCard(){
    	return $this->belongsTo(MemberCard::class, 'member_card_id','id');
    }

//    public function scopeOrderCancel(Builder $query)
//    {
//        return $query->has('cancel', 'and', function ($q)
//            {
//                $q->where('status', config('constant.cancelStatus.going'));
//            }
//        );
//    }

    public function royaltyI()
    {
        return $this->morphMany(Royalty::class, 'item');
    }
}
